//
//  PasswordViewController.m
//  jang
//
//  Created by lee han on 03/04/2017.
//  Copyright © 2017 lee han. All rights reserved.
//

#import "PasswordViewController.h"
#import "CRUserData.h"
#import "CommonView.h"

@interface PasswordViewController ()

@end

@implementation PasswordViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    [self.navigationItem.backBarButtonItem setTitle:@""];
    
    _mPassword.delegate = self;
    _mConfirm.delegate = self;
    _mVerifacation.delegate = self;
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}
- (IBAction)onClickVerification:(id)sender {
    //获取验证码
    
    NSMutableDictionary* data = [NSMutableDictionary dictionaryWithObjectsAndKeys:
                                 [CRUserData sharedInstance].m_landingPhone,  @"phone",
                                 nil];
    
    [[network sharedInstance] startHttpRequest:self Data:data functionType:NetworkFunctionType_GetVerifyCode];
}
- (IBAction)onClickSubmit:(id)sender {
    if(_mPassword.text.length == 0){
        [CommonView addAlertView:self Text:@"请输入密码"];
        return;
    }
    
    if(_mConfirm.text != _mPassword.text){
        [CommonView addAlertView:self Text:@"密码输入不一致"];
        return;
    }
    
    if(_mVerifacation.text.length == 0){
        [CommonView addAlertView:self Text:@"请输入验证码"];
        return;
    }
    
    NSMutableDictionary* data = [NSMutableDictionary dictionaryWithObjectsAndKeys:
                                 _mPassword.text, @"pwd",
                                 _mVerifacation.text, @"vcode",
                                 nil];
    
    [[network sharedInstance] startHttpRequest:self Data:data functionType:NetworkFunctionType_ModifyPassword];
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/
#pragma mark networkDelegate
- (void)parserResult:(NSDictionary *)response functionType:(NSInteger)type
{
    if(type == NetworkFunctionType_ModifyPassword) {
        if([[response objectForKey:@"status"] integerValue] == 1){
            [CRUserData sharedInstance].m_token = [response objectForKey:@"token"];
            //登陆成功
            [CRUserData sharedInstance].m_password = self.mPassword.text;
            [[CRUserData sharedInstance] saveLoginInfo];
            
            [CommonView gotoViewController:self Target:@"main"];
        }
        else{
            [CommonView addAlertView:self Text:@"修改密码失败"];
        }
    }
    else if (type == NetworkFunctionType_GetVerifyCode) {
        if([[response objectForKey:@"status"] integerValue] != 1) {
            [CommonView addAlertView:self Text:@"获取验证码失败"];
        }
    }
    
}
//end networkDelegate

#pragma mark UITextFieldDelegate
-(BOOL)textFieldShouldReturn:(UITextField *)textField{
    if(textField == _mPassword){
        [_mConfirm becomeFirstResponder];
    }
    else if (textField == _mConfirm){
        [_mVerifacation becomeFirstResponder];
    }
    else if (textField == _mVerifacation){
        [textField resignFirstResponder];
    }
    return YES;
}
//end UITextFieldDelegate
@end
